"""
简化的订单API - 用于快速解决前端问题
"""
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from datetime import datetime
import json

router = APIRouter(prefix="/api/orders", tags=["订单管理"])

class OrderCreateRequest(BaseModel):
    """订单创建请求模型"""
    goods_ids: list[int]
    quantities: list[int]
    address_id: int = None
    remark: str = ""

@router.post("/create", summary="创建订单")
async def create_order(order_request: OrderCreateRequest):
    """创建订单（简化版本）"""
    try:
        # 验证数据
        if len(order_request.goods_ids) != len(order_request.quantities):
            raise HTTPException(
                status_code=400,
                detail="商品ID和数量列表长度不匹配"
            )
        
        # 生成订单号
        timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
        import random
        random_num = random.randint(1000, 9999)
        order_no = f"XHS{timestamp}{random_num}"
        
        # 模拟订单处理
        print(f"收到订单: {order_no}")
        print(f"商品: {order_request.goods_ids}")
        print(f"数量: {order_request.quantities}")
        print(f"备注: {order_request.remark}")
        
        return {
            "code": 200,
            "message": "订单创建成功，正在处理中",
            "data": {
                "order_no": order_no,
                "status": "pending"
            }
        }
        
    except HTTPException:
        raise
    except Exception as e:
        print(f"创建订单失败: {e}")
        raise HTTPException(
            status_code=500,
            detail="订单创建失败"
        )

@router.get("/status/{order_no}", summary="查询订单状态")
async def get_order_status(order_no: str):
    """查询订单状态"""
    return {
        "code": 200,
        "message": "查询成功",
        "data": {
            "order_no": order_no,
            "status": "processing",
            "message": "订单正在处理中..."
        }
    }

@router.get("/test-connection", summary="测试连接")
async def test_connection():
    """测试连接"""
    return {
        "code": 200,
        "message": "连接测试成功",
        "data": {
            "host": "127.0.0.1",
            "port": 8000,
            "queue": "simple_queue"
        }
    }
